<?php

class MailConfigController extends BaseController
{
	public function actionAdmin()
	{
		//获取配置
		$config = F::setting("mail_config");
		$mailSentType = F::setting("mail_localhost");
		//配置反系列化
		$config = unserialize($config);
		
		if(isset($_POST['Congfig'])){
			//邮件服务配置
			$model = Setting::model()->findByAttributes(array('zkl_variable'=>'mail_config'));
			//是否本地邮件服务器,保存配置
			$mailType = Setting::model()->findByAttributes(array('zkl_variable'=>'mail_localhost'));
			$mailType->zkl_valued = $_POST['mail_sent_type'];
			$mailType->save();
			
			//mail配置数据处理，去空格
			$value = $_POST['Congfig'];
			foreach($value as &$val){
				$val = trim($val);
			}
			//加固定值
			$value['CharSet'] = 'UTF-8';
			$value['ContentType'] = 'text/html';
			//将数据系列化存储
			$model->zkl_valued = serialize($value);
			if($model->save()){
				//成功跳转
				$this->redirect(Yii::app()->request->urlReferrer);
			}
		}
		
		//渲染视图
		$this->render('admin',array(
					'config'=>$config,
					'mailSentType'=>$mailSentType
			));
	}
	
	/**
	 * 测试邮件
	 */
	public function actionTest(){
		//发送邮件类型
		$sentType = $_POST['mail_sent_type'];
		
		$testmail = $_POST['testmail'];

		//mail配置数据处理，去空格
		$value = $_POST['Congfig'];
		foreach($value as &$val){
			$val = trim($val);
		}

		
		//实例化模型
		$mail = Yii::app()->mailer;
		foreach($value as $key=>$val){
			$mail->_myMailer->$key = $val;
		}
		//加固定值
		$mail->_myMailer->CharSet = "UTF-8";
		$mail->_myMailer->ContentType = "text/html";
		$mail->_myMailer->Password = trim($_POST['Congfig']['Password']);
		if($sentType == 'smtp'){
			$mail->_myMailer->IsSMTP();
			$mail->_myMailer->SMTPAuth = true;
		}

		$mail->AddReplyTo($testmail);
		$mail->AddAddress($testmail);
		$mail->Subject = "这是一封来自".Yii::app()->name."的测试邮件";
		$params['message'] = '收到比邮件说明配置正确！';
		$mail->getView('message', array('params'=>$params,'mailer'=>$mail),'member');
		if($mail->Send()){
			//成功跳转
			echo 1;
			exit;
		}
	}
}